package com.ruoyi.print.controller;

import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.print.domain.CustomerProvider;
import com.ruoyi.print.service.ICustomerProviderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 自定义providerController
 *
 * @author korol
 * @date 2023-11-06
 */
@Api(tags = "自定义provider管理")
@RestController
@RequestMapping("/customProvider/provider")
public class CustomerProviderController extends BaseController {
    @Autowired
    private ICustomerProviderService customerProviderService;

    /**
     * 查询自定义provider列表
     */
    @ApiOperation("查询自定义provider列表")
    @PreAuthorize("@ss.hasPermi('customProvider:provider:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerProvider customerProvider) {
        startPage();
        List<CustomerProvider> list = customerProviderService.selectCustomerProviderList(customerProvider);
        return getDataTable(list);
    }

    /**
     * 导出自定义provider列表
     */
    @ApiOperation("导出自定义provider列表")
    @PreAuthorize("@ss.hasPermi('customProvider:provider:export')")
    @Log(title = "自定义provider", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerProvider customerProvider) {
        List<CustomerProvider> list = customerProviderService.selectCustomerProviderList(customerProvider);
        ExcelUtil<CustomerProvider> util = new ExcelUtil<CustomerProvider>(CustomerProvider.class);
        util.exportExcel(response, list, "自定义provider数据");
    }

    /**
     * 获取自定义provider详细信息
     */
    @ApiOperation("获取自定义provider详细信息")
    @PreAuthorize("@ss.hasPermi('customProvider:provider:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(customerProviderService.getById(id));
    }

    /**
     * 新增自定义provider
     */
    @ApiOperation("新增自定义provider")
    @PreAuthorize("@ss.hasPermi('customProvider:provider:add')")
    @Log(title = "自定义provider", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerProvider customerProvider) {
        return toAjax(customerProviderService.save(customerProvider));
    }

    /**
     * 修改自定义provider
     */
    @ApiOperation("修改自定义provider")
    @PreAuthorize("@ss.hasPermi('customProvider:provider:edit')")
    @Log(title = "自定义provider", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerProvider customerProvider) {
        return toAjax(customerProviderService.updateById(customerProvider));
    }

    /**
     * 删除自定义provider
     */
    @ApiOperation("删除自定义provider")
    @PreAuthorize("@ss.hasPermi('customProvider:provider:remove')")
    @Log(title = "自定义provider", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(customerProviderService.removeByIds(Arrays.asList(ids)));
    }


    @ApiOperation("根据type 获取自定义provider")
    @Anonymous
    @GetMapping("/list-provider-by-type/{type}")
    public AjaxResult listProviderByType(@PathVariable String type) {
        return AjaxResult.success(customerProviderService.list(new LambdaQueryWrapper<CustomerProvider>()
                .eq(CustomerProvider::getType, type)));
    }
}
